package com.steven.im.myinterface;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.webkit.JavascriptInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steven.im.App;
import com.steven.im.ui.activity.GoodsDetailActivity;
import com.steven.im.ui.activity.LoginAndRegisterActivity;
import com.steven.im.ui.activity.MainActivity;
import com.steven.im.ui.activity.RechargeActivity;
import com.steven.im.ui.activity.WebActivity;
import com.steven.im.utils.ShowShareUtils;
import com.steven.im.utils.ToastUtil;
import com.steven.im.utils.UserUtils;

/**
 * Created by Steven on 2019/2/26.
 */
public class JsInterface {
    private FragmentActivity activity;

    /**
     * Instantiate the interface and set the context
     */
    public JsInterface(FragmentActivity activity) {
        this.activity = activity;
    }

    @JavascriptInterface
    public void toURL(String actionUrl) {

        activity.startActivity(new Intent(activity, WebActivity.class).putExtra("url", actionUrl + ""));
    }

    @JavascriptInterface
    public void toLogin() {
        if (!UserUtils.checkLogin())
            activity.startActivity(new Intent(activity, LoginAndRegisterActivity.class));
    }

    @JavascriptInterface
    public void toAccount() {
        activity.startActivity(new Intent(activity, MainActivity.class).putExtra("tab", 3));
        activity.finish();
    }

    @JavascriptInterface
    public void toShare(String actionUrl) {
        JSONObject obj = (JSONObject) JSON.parse(actionUrl + "");
        ShowShareUtils.showShareDialog(activity, activity.getSupportFragmentManager(), obj);
    }

    @JavascriptInterface
    public void toGoods(String actionUrl) {
        activity.startActivity(new Intent(activity, GoodsDetailActivity.class).putExtra("issuedNo", actionUrl + ""));
    }

    @JavascriptInterface
    public void toCategory(String actionUrl) {
        activity.startActivity(new Intent(activity, MainActivity.class).putExtra("tab", 1).putExtra("actionUrl", actionUrl));
        activity.finish();
    }

    @JavascriptInterface
    public void toRecharge(String actionUrl) {
        if (UserUtils.checkLogin()) {
            activity.startActivity(new Intent(activity, RechargeActivity.class).putExtra("money", actionUrl + ""));
        } else {
            activity.startActivity(new Intent(activity, LoginAndRegisterActivity.class));
        }
    }

    @JavascriptInterface
    public void toIndex() {
        activity.startActivity(new Intent(activity, MainActivity.class).putExtra("tab", 0));
        activity.finish();
    }

    @JavascriptInterface
    public void toMyShare() {
        ToastUtil.showShort(App.getContext(), "邀请好友");
    }
}
